package com.yangjie.user.service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.yangjie.common.dao.Dao;
import com.yangjie.common.service.AbstractService;
import com.yangjie.user.dao.UserDao;
import com.yangjie.user.domain.User;

@Component("userService")
public class UserService extends AbstractService<User> {
	Logger logger = Logger.getLogger(this.getClass());
	@Inject
	UserDao dao = null;

	@Inject
	private PasswordEncoder passwordEncoder;

	public Dao getDao() {
		return dao;
	}

//	@PreAuthorize("hasRole('USER_CREATE')")
	public void create(Object userObj) {
		User user = (User) userObj;
		System.out.println("create...........");
		if (user.getPassword() != null) {
			user.setPassword(passwordEncoder.encode(user.getPassword()));
		}
		super.create(user);
	}


	public User findUserByUsername(String username) {
		try {
			logger.debug("findUserByUsername:" + username);
			return dao.findUserByUsername(username);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@PreAuthorize("permitAll")
	public List<User> findAllNonAdminUsers() {
		try {
			logger.debug("begin of findAllNonAdminUsers");
			List<User> retval =  dao.findAllNonAdminUsers();
			logger.debug("end of findAllNonAdminUsers");
			return retval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {

			Locale locale = Locale.ENGLISH;
			Locale locale2 = new Locale("zh");
			System.out.println(locale.getCountry() + ":" + locale.getLanguage());
			System.out.println(locale2.getCountry() + ":" + locale2.getLanguage());

	}
}
